package eav_service

import (
	"context"
	"time"

	"rdiot-demo/internal/eav/domain"

	"github.com/google/uuid"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

// TemplateService 模板服务接口
type TemplateService interface {
	CreateTemplate(ctx context.Context, template *domain.EntityTemplate) error
	GetTemplate(ctx context.Context, id uuid.UUID) (*domain.EntityTemplate, error)
	UpdateTemplate(ctx context.Context, template *domain.EntityTemplate) error
	DeleteTemplate(ctx context.Context, id uuid.UUID) error
	ListTemplates(ctx context.Context, filter TemplateFilter) ([]domain.EntityTemplate, int64, error)
}

// EntityService 实体服务接口
type EntityService interface {
	CreateEntity(ctx context.Context, req CreateEntityRequest) (*domain.Entity, error)
	GetEntityByID(ctx context.Context, id uuid.UUID) (*domain.Entity, error)
	GetEntitiesByTemplate(ctx context.Context, templateID uuid.UUID, page, size int) ([]*domain.Entity, int64, error)
	UpdateEntity(ctx context.Context, id uuid.UUID, req UpdateEntityRequest) (*domain.Entity, error)
	DeleteEntity(ctx context.Context, id uuid.UUID) error
	QueryEntities(ctx context.Context, req QueryEntitiesRequest) ([]*domain.Entity, int64, error)
	BatchCreateEntities(ctx context.Context, req BatchCreateEntitiesRequest) ([]*domain.Entity, error)
	GetEntityWithAttributes(ctx context.Context, id uuid.UUID) (*EntityWithAttributes, error)
}

// EAV Services 服务集合
type EAVServices struct {
	TemplateService TemplateService
	EntityService   EntityService
}

// EAVServices 创建新的 EAV Services 服务集合
func NewEAVServices(db *gorm.DB, logger *zap.Logger) *EAVServices {
	templateService := NewTemplateService(db, logger)
	entityService := NewEntityService(db, logger)

	return &EAVServices{
		TemplateService: templateService,
		EntityService:   entityService,
	}
}

// TemplateFilter 模板过滤器
type TemplateFilter struct {
	Name     string
	IsActive *bool
	Page     int
	Size     int
}

// CreateEntityRequest 创建实体请求
type CreateEntityRequest struct {
	TemplateID uuid.UUID              `json:"template_id" validate:"required"`
	Name       string                 `json:"name" validate:"required,min=1,max=255"`
	Attributes map[string]interface{} `json:"attributes,omitempty"`
}

// UpdateEntityRequest 更新实体请求
type UpdateEntityRequest struct {
	Name       *string                `json:"name,omitempty" validate:"omitempty,min=1,max=255"`
	Attributes map[string]interface{} `json:"attributes,omitempty"`
}

// QueryEntitiesRequest 查询实体请求
type QueryEntitiesRequest struct {
	TemplateID *uuid.UUID           `json:"template_id,omitempty"`
	Name       *string              `json:"name,omitempty"`
	Conditions []AttributeCondition `json:"conditions,omitempty"`
	OrderBy    []OrderByClause      `json:"order_by,omitempty"`
	Page       int                  `json:"page" validate:"min=1"`
	Size       int                  `json:"size" validate:"min=1,max=100"`
}

// AttributeCondition 属性查询条件
type AttributeCondition struct {
	AttributeName string      `json:"attribute_name" validate:"required"`
	Operator      string      `json:"operator" validate:"required,oneof=eq ne gt gte lt lte like in"`
	Value         interface{} `json:"value" validate:"required"`
	DataType      string      `json:"data_type" validate:"required,oneof=string number boolean datetime reference array json"`
}

// OrderByClause 排序子句
type OrderByClause struct {
	Field     string `json:"field" validate:"required"`
	Direction string `json:"direction" validate:"required,oneof=asc desc"`
}

// BatchCreateEntitiesRequest 批量创建实体请求
type BatchCreateEntitiesRequest struct {
	TemplateID uuid.UUID          `json:"template_id" validate:"required"`
	Entities   []CreateEntityData `json:"entities" validate:"required,min=1,max=100"`
}

// CreateEntityData 创建实体数据
type CreateEntityData struct {
	Name       string                 `json:"name" validate:"required,min=1,max=255"`
	Attributes map[string]interface{} `json:"attributes,omitempty"`
}

// EntityWithAttributes 包含属性的实体
type EntityWithAttributes struct {
	Entity     *domain.Entity         `json:"entity"`
	Template   *domain.EntityTemplate `json:"template"`
	Attributes map[string]interface{} `json:"attributes"`
	CreatedAt  time.Time              `json:"created_at"`
	UpdatedAt  time.Time              `json:"updated_at"`
}
